class WordPuzzleController < ApplicationController
  
  skip_filter :ensure_is_authenticated_in_fb
  
  def update_score
    user = User.find(params[:user_id])
    enigma_id = params[:id]
    points = params[:points]
    seconds = params[:seconds]
    minutes = params[:minutes]
    hours = params[:hours]
    enigma = Enigma.find(enigma_id)
    
    um = UserMatch.find_by_user_id_and_enigma_id_and_finished(user.id, enigma_id, false )
    if um
      logger.debug("Updating UserMatch...")
      um.finished=true
      um.points = points
      um.seconds = seconds.to_i+(60*minutes.to_i)+(60*60*hours.to_i)
      um.save!
      user.points += points.to_i
      if points.to_i > 0 and (um.seconds < user.best_time or user.best_time==0) 
        user.best_time = um.seconds
        
      end
      user.played_games +=1
      user.save!
      render :update do |page|
        page << build_js_wall_post(enigma, user, um)
      end
    end   
    
    
    #render :js=>
  end
  
  
  def new
    user = User.find(params[:user_id])
    enigma = Enigma.random(user)
    #Start a match
    um = UserMatch.new
    um.user_id = user.id
    um.enigma_id = enigma.id
    um.save!
    finish_game_url = update_score_user_word_puzzle_path(current_user, enigma)
    out = {:enigma=>enigma, :finish_game_url=>finish_game_url}
    render :json=>out.to_json
  end
  
end
